import React from 'react';
import { BrowserRouter as  Router, Route, Switch,Redirect} from "react-router-dom";
// import {connect } from 'dva/router'
import {connect} from 'react-redux'
import App from './../App'
import Admin from './../admin'
import Login from '../components/login/Login'
import Routers from './routerData'
// 导入路由
// import  TabsBasic from './../components/table/basic';

class SetRouter extends React.Component {
    constructor(props) {
        super(props);
        this.state ={

        }
    }

    render() {
        let token = this.props.token
        console.log(token,'token')
        return (
            <Router>
                <App>
                <Route path="/login" component={Login}/>
                <Route path="/admin" render={()=>
                    <Admin>
                        <Switch>
                            {Routers.map((item, index) => {
                                console.log(item)
                                return <Route key={index} path={item.path} exact render={props =>
                                        (!item.auth ? (<item.component {...props} />) : (token ? <item.component {...props} /> : <Redirect to={{
                                            pathname: '/login',state: { from: props.location }}} />)
                                            )
                                    } />
                            })}
                        </Switch>
                    </Admin>
                } />
                </App>
            </Router>
        );
    }
}

// redux拿到token并挂载到App的props上面
const mapStateToProps = (state, ownProps) => {
       return { token: state.token }
}


export default connect(mapStateToProps)(SetRouter);